 <!-- 商家销量统计的柱状图 -->
<template>
  <div class="com-container" @dblclick="revertMap">
    <div class="con-chart" ref="map_ref"></div>
  </div>
</template>

<script>
import axios from "axios";
import { mapState } from "vuex";
import { getProvinceMapInfo } from "../utils/map_utils";
export default {
  data() {
    return {
      chartInstance: null,
      allData: [],
      mapData: {}, //获取的省份地图数据
    };
  },
  created() {
    // console.log(this.$nextTick);
    //在组件创建完成后，进行回调函数的注册
    this.$socket.registerCallBack("mapData", this.getData);
  },
  mounted() {
    //dom元素挂载完成
    this.initChart();
    // this.getData();
    //发送数据给服务器,获取数据
    this.$socket.send({
      action: "getData",
      socketType: "mapData",
      chartName: "map",
      value: "",
    });
    window.addEventListener("resize", this.screenAdapter);
    //在页面加载完成时,主动进行屏幕的适配
    this.screenAdapter();
  },
  destroyed() {
    //组件销毁时,取消监听器
    window.removeEventListener("resize", this.screenAdapter);
    //在组件销毁后，进行回调函数的移除
    this.$socket.removeCallBack("mapData");
  },
  watch: {
    theme() {
      this.chartInstance.dispose(); //销毁当前图表
      this.initChart(); //重新初始化新主题的图表
      this.updateChart(); //更新图表
      this.screenAdapter(); //完成图表的适配
    },
  },
  computed: {
    ...mapState(["theme"]),
  },
  methods: {
    //初始化echarts
    async initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.map_ref, this.theme);
      //获取中国的矢量地图数据
      //http://localhost:8999/static/map/china.json
      //由于我们现在获取的地图数据并不是位于koa的后台,所以不能用this.$http.get获取
      const ret = await axios.get(
        "http://localhost:8999/static/map/china.json"
      );
      //将地图的矢量数据注册给全局的echart对象
      this.$echarts.registerMap("china", ret.data);
      // console.log(ret);
      //地图的配置
      const initOption = {
        title: {
          text: "▎商家分布",
          left: 20,
          top: 20,
        },
        legend: {
          left: "5%",
          bottom: "5%",
          orient: "vertical",
        },
        geo: {
          type: "map",
          map: "china",
          top: "5%",
          bottom: "5%",
          itemStyle: {
            areaColor: "#2E72BF",
            borderColor: "#333",
          },
        },
      };
      this.chartInstance.setOption(initOption);
      //监听地图点击事件
      this.chartInstance.on("click", async (arg) => {
        // console.log(arg);
        const provinceInfo = getProvinceMapInfo(arg.name);
        console.log(provinceInfo);
        //需要获取这个省份的地图适量数据
        //判断当前点击的省份地图矢量数据在mapData中是否存在
        if (!this.mapData[provinceInfo.key]) {
          const ret = await axios.get(
            "http://localhost:8999" + provinceInfo.path
          );
          this.mapData[provinceInfo.key] = ret.data;
          //将省份地图数据注册到地图上
          this.$echarts.registerMap(provinceInfo.key, ret.data);
        }
        const changeOption = {
          geo: {
            map: provinceInfo.key,
          },
        };
        this.chartInstance.setOption(changeOption);
      });
    },
    //获取数据
    async getData(ret) {
      //获取服务器的数据
      // const { data: ret } = await this.$http.get("/map");
      this.allData = ret;
      // console.log(this.allData);
      this.updateChart();
    },
    //更新图表
    updateChart() {
      //处理图表需要的数据
      //图例的数据
      const legendArr = this.allData.map((item) => {
        return item.name;
      });
      const seriesArr = this.allData.map((item) => {
        //return的这个对象就代表一个类别下所代表的所有散点数据
        // 如果想在地图中显示散点的数据,所以我们需要给散点的图表增加一个配置coordinateSystem:geo
        return {
          type: "effectScatter",
          rippleEffect: {
            scale: 5,
            brushType: "stroke",
          },
          name: item.name,
          data: item.children,
          coordinateSystem: "geo",
        };
      });
      const dataOption = {
        legend: {
          data: legendArr,
        },
        series: seriesArr,
      };
      this.chartInstance.setOption(dataOption);
    },
    //当浏览器的大小发生变化时,调用该函数,完成屏幕适配
    screenAdapter() {
      //图表屏幕分辨率的配置
      const titleFontSize = (this.$refs.map_ref.offsetWidth / 100) * 3.2;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
          legend: {
            itemWidth: titleFontSize / 2,
            itemHeight: titleFontSize / 2,
            itemGap: titleFontSize / 2,
            textStyle: {
              fontSize: titleFontSize / 2,
            },
          },
        },
      };
      this.chartInstance.setOption(adapterOption);
      //手动调用图表resize
      this.chartInstance.resize();
    },
    //双击回到中国地图
    revertMap() {
      //重置为中国地图
      const revertOption = {
        geo: {
          map: "china",
        },
      };
      this.chartInstance.setOption(revertOption);
    },
  },
};
</script>

<style>
</style>